package com.smarthealthring.mm.utils.loading;

import android.content.Context;
import android.os.Looper;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.smarthealthring.mm.R;

/**
 * Created by zhy on 15/8/26.
 */
public class LoadingAndRetryLayout extends FrameLayout {
    private SwipeRefreshLayout mLoadingView;
    private View mRetryView;
    private FrameLayout mContentView;
    private View mEmptyView;
    private LayoutInflater mInflater;

    private static final String TAG = LoadingAndRetryLayout.class.getSimpleName();


    public LoadingAndRetryLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mInflater = LayoutInflater.from(context);

        View view = mInflater.inflate(R.layout.base_retry, this);
        mRetryView = view.findViewById(R.id.base_ll_error);
        mEmptyView = view.findViewById(R.id.base_ll_empty);
        mContentView = (FrameLayout) view.findViewById(R.id.base_fl_content);
        mLoadingView = (SwipeRefreshLayout) view.findViewById(R.id.base_swipeLayout);

        mLoadingView.setColorSchemeColors(ContextCompat.getColor(context, R.color.colorAccent));
    }


    public LoadingAndRetryLayout(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LoadingAndRetryLayout(Context context) {
        this(context, null);
    }

    private boolean isMainThread() {
        return Looper.myLooper() == Looper.getMainLooper();
    }

    public void showLoading() {
        if (isMainThread()) {
            showView(mLoadingView);
        } else {
            post(() -> showView(mLoadingView));
        }
    }

    public void showRetry() {
        if (isMainThread()) {
            showView(mRetryView);
        } else {
            post(() -> showView(mRetryView));
        }
    }

    public void showContent() {
        if (isMainThread()) {
            showView(mContentView);
        } else {
            post(() -> showView(mContentView));
        }
    }

    public void showEmpty() {
        if (isMainThread()) {
            showView(mEmptyView);
        } else {
            post(() -> showView(mEmptyView));
        }
    }

    private boolean hasData;

    private void showView(View view) {
        if (view == null)
            return;

        if (view == mLoadingView) {
            mLoadingView.setVisibility(View.VISIBLE);
            mLoadingView.setRefreshing(true);
            if (mRetryView != null)
                mRetryView.setVisibility(View.GONE);
            if (mContentView != null){
                if(!hasData){
                    mContentView.setVisibility(View.GONE);
                }else{
                    mContentView.setVisibility(View.VISIBLE);
                }
            }

            if (mEmptyView != null)
                mEmptyView.setVisibility(View.GONE);
        } else if (view == mContentView) {
            mContentView.setVisibility(View.VISIBLE);
            hasData = true;

            if (mLoadingView != null)
                mLoadingView.setRefreshing(false);
            if (mRetryView != null)
                mRetryView.setVisibility(View.GONE);
            if (mEmptyView != null)
                mEmptyView.setVisibility(View.GONE);
        } else if (view == mRetryView) {
            hasData = false;
            mRetryView.setVisibility(View.VISIBLE);
            if (mLoadingView != null){
                mLoadingView.setRefreshing(false);
                mLoadingView.setVisibility(View.GONE);
            }
            if (mContentView != null)
                mContentView.setVisibility(View.GONE);
            if (mEmptyView != null)
                mEmptyView.setVisibility(View.GONE);
        } else if (view == mEmptyView) {
            hasData = false;
            mEmptyView.setVisibility(View.VISIBLE);
            if (mLoadingView != null){
                mLoadingView.setRefreshing(false);
                mLoadingView.setVisibility(View.GONE);
            }
            if (mRetryView != null)
                mRetryView.setVisibility(View.GONE);
            if (mContentView != null)
                mContentView.setVisibility(View.GONE);
        }
    }


    public View setLoadingView(int layoutId) {
        return setLoadingView(mInflater.inflate(layoutId, this, false));
    }

    public View setEmptyView(int layoutId) {
        return setEmptyView(mInflater.inflate(layoutId, this, false));
    }

    public View setRetryView(int layoutId) {
        return setRetryView(mInflater.inflate(layoutId, this, false));
    }

    public View setContentView(int layoutId) {
        return setContentView(mInflater.inflate(layoutId, this, false));
    }

    public View setLoadingView(View view) {
        View loadingView = mLoadingView;
        if (loadingView != null) {
            Log.w(TAG, "you have already set a loading view and would be instead of this new one.");
        }
        removeView(loadingView);
        addView(view);
        mLoadingView = (SwipeRefreshLayout) view;
        return mLoadingView;
    }

    public View setEmptyView(View view) {
        View emptyView = mEmptyView;
        if (emptyView != null) {
            Log.w(TAG, "you have already set a empty view and would be instead of this new one.");
        }
        removeView(emptyView);
        addView(view);
        mEmptyView = view;
        return mEmptyView;
    }

    public View setRetryView(View view) {
        View retryView = mRetryView;
        if (retryView != null) {
            Log.w(TAG, "you have already set a retry view and would be instead of this new one.");
        }
        removeView(retryView);
        addView(view);
        mRetryView = view;
        return mRetryView;

    }

    public View setContentView(View view) {
        FrameLayout contentView = mContentView;
        if (contentView != null) {
            Log.w(TAG, "you have already set a retry view and would be instead of this new one.");
        }
        //        removeView(contentView);
        contentView.addView(view,view.getLayoutParams());
        return mContentView;
    }

    public View getRetryView() {
        return mRetryView;
    }

    public SwipeRefreshLayout getLoadingView() {
        return mLoadingView;
    }

    public View getContentView() {
        return mContentView;
    }

    public View getEmptyView() {
        return mEmptyView;
    }
}
